package yl.hs.bmipfull.service.chart.impl;

import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratory.IProductCateDao;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.daos.production.IProductShipOrderDao;
import yl.hs.bmipfull.daos.production.IProductionLineDao;
import yl.hs.bmipfull.daos.production.IProductionRecordDao;
import yl.hs.bmipfull.daos.production.IProductionTaskDao;
import yl.hs.bmipfull.daos.salemanage.IBuildProjectInfoDao;
import yl.hs.bmipfull.pojo.production.ProductShipJhEntity;
import yl.hs.bmipfull.pojo.salemanage.BuildProjectInfoEntity;
import yl.hs.bmipfull.service.chart.IProductionChartService;
import yl.hs.bmipfull.utils.DateUtil;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.chart.SiteProductionDegreeVO;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 谢东
 * @apiNote
 * @since 2022-03-16 14:09 创建
 */
@Service
public class ProductionChartServiceImpl implements IProductionChartService {

    @Autowired
    IProductionTaskDao productionTaskDao;
    @Autowired
    IProductShipOrderDao productShipOrderDao;
    @Autowired
    IProductCateDao cateDao;
    @Autowired
    IBuildProjectInfoDao projectInfoDao;
    @Autowired
    IProductionLineDao productionLineDao;
    @Autowired
    IProductionRecordDao  productionRecordDao;
    @Override
    public BigDecimal getPlanQuantity(int type, String cateSn) {
        BigDecimal result = BigDecimal.ZERO;
        if (type == 1) {
            result = productionTaskDao.getPlanQuantity(cateSn, type);
        } else if (type == 2) {
            result = productionTaskDao.getPlanQuantity(cateSn, type);
        } else if (type == 3) {
            result = productionTaskDao.getPlanQuantity(cateSn, type);
        } else {
            result = productionTaskDao.getPlanQuantity(cateSn, 2);
        }
        return result;
    }

    @Override
    public BigDecimal getDeliveringAmount(int type, String cateSn) {
        BigDecimal result = BigDecimal.ZERO;
        if (type == 1) {
            result = productShipOrderDao.getDeliveringAmount(cateSn, type);
        } else if (type == 2) {
            result = productShipOrderDao.getDeliveringAmount(cateSn, type);
        } else {
            result = productShipOrderDao.getDeliveringAmount(cateSn, 2);
        }
        return result;
    }

    @Override
    public Integer getNoProportioning() {
        return productionTaskDao.getNoProportioning();
    }

    @Override
    public List<SiteProductionDegreeVO> getSiteProductionDegree() {
        return productionTaskDao.getSiteProductionDegree();
    }

    @Override
    public Map<String, Object> getProductByMonth() {
        Map<String, Object> result = new HashMap<>();
        val productCateEntities = cateDao.queryList(new HashMap<>());
        List<String> catename = new ArrayList<>();
        List<Double> catenumber = new ArrayList<Double>();
        productCateEntities.forEach(s -> {
            catename.add(s.getCateName());
            var number = productShipOrderDao.getMonthVolume(new Date(), s.getSn());
            catenumber.add(number);
        });
        result.put("xAxis", catename);
        result.put("data", catenumber);
        return result;
    }

    @Override
    public Map<String, Object> getProductProjectByMonth() {
        Map<String, Object> result = new HashMap<>();
        List<String> projectname = new ArrayList<>();
        List<BigDecimal> projectnumber = new ArrayList<BigDecimal>();
        var list = productShipOrderDao.getByProjectByMonth();
        if (list == null || list.size() == 0) {
            var listentity = projectInfoDao.queryList(new HashMap<>(), null);
            for (var i = 0; i < 10; i++) {
                if(listentity.size()>i){
                    projectname.add(listentity.get(i).getProjectName());
                    projectnumber.add(BigDecimal.valueOf(0));
                }else{
                    break;
                }
            }
        } else {
            list.forEach(s -> {
                projectname.add(s.getProjectName());
                projectnumber.add(s.getQuantity());
            });
        }
        result.put("xAxis", projectname);
        result.put("data", projectnumber);
        return result;
    }

    @Override
    public Map<String, Object> getProductSalesCurve(String productCateSN, int type) {
        Map<String,Object> map = new HashMap<>();
        map.put("sn",productCateSN);
        var sn = productCateSN;
        List<Object> legend = new ArrayList<>();
        List<String> xAxis = new ArrayList<>();
        List<Object> data = new ArrayList<>();
        List<Object> data2 = new ArrayList<>();
        List<Object> data3 = new ArrayList<>();
        var cate = cateDao.queryList(map);
        cate.forEach(s->{
            legend.add(s.getCateName()+"销售量");
            legend.add(s.getCateName()+"生产量");
        });
        switch (type){
            case 1:
                List<String> dayList= DateUtil.getNearlyMonthDates();
                List<Object> object = new ArrayList<>();
                for (var day:dayList) {
                    data.add(productShipOrderDao.getProductSalesCurveByDate(1,day,sn));
                    data2.add(productionRecordDao.getSumActualOutputByDate(1,day,sn));
                }
                break;
            case 2:
                int nowWeek=DateUtil.getWeek(new Date());
                for (int i=1;i<8;i++){
                    data.add(productShipOrderDao.getProductSalesCurveByDate(2, String.valueOf(nowWeek-7+i),sn));
                    data2.add(productionRecordDao.getSumActualOutputByDate(2,String.valueOf(nowWeek-7+i),sn));
                }
                break;
            case 3:
                List<String> monthList=DateUtil.getNearlyYearDates();
                for (var month:monthList) {
                    data.add(productShipOrderDao.getProductSalesCurveByDate(3, month,sn));
                    data2.add(productionRecordDao.getSumActualOutputByDate(3,month,sn));
                }
                break;
        }
        data3.add(data);
        data3.add(data2);
        xAxis=getXAxisDateByDateType(type);
        map.put("legend",legend);
        map.put("data",data3);
        map.put("xAxis",xAxis);
        return map;
    }

    @Override
    public Map<String, Object> getProductionLineSalesVolumeAndOutput(int dateType,String cateSN){
        Map<String,Object> result=new HashMap<>();
        var lineList=productionLineDao.getListByUseRang(cateSN);
        List<String> legend=new ArrayList<>();
        List<Object> data=new ArrayList<>();
        List<String> xAxis=new ArrayList<>();
        if (lineList.size()>0){
            for (var line : lineList) {
                List<BigDecimal> xsl=new ArrayList<>();
                List<BigDecimal> scl=new ArrayList<>();
                legend.add(line.getPlName()+"销售量");
                legend.add(line.getPlName()+"生产量");
                if (dateType==1){
                    //获取近一个月中的日期
                    List<String> dayList= DateUtil.getNearlyMonthDates();
                    for (var day:dayList) {
                        xsl.add(productShipOrderDao.getSumQuantityByLineSNAndDate(1,day,line.getSn()));
                        scl.add(productionRecordDao.getSumActualOutputByLineSNAndDate(1,day,line.getSn()));
                    }
                }else if (dateType==2){
                    //获取当前时间为第几周
                    int nowWeek=DateUtil.getWeek(new Date());
                    for (int i=1;i<8;i++){
                        xsl.add(productShipOrderDao.getSumQuantityByLineSNAndDate(2, String.valueOf(nowWeek-7+i),line.getSn()));
                        scl.add(productionRecordDao.getSumActualOutputByLineSNAndDate(2,String.valueOf(nowWeek-7+i),line.getSn()));
                    }
                }else if (dateType==3){
                    //获取近一年的月份
                    List<String> monthList=DateUtil.getNearlyYearDates();
                    for (var month:monthList) {
                        xsl.add(productShipOrderDao.getSumQuantityByLineSNAndDate(3, month,line.getSn()));
                        scl.add(productionRecordDao.getSumActualOutputByLineSNAndDate(3,month,line.getSn()));
                    }
                }
                data.add(xsl);
                data.add(scl);
            }
        }
        xAxis=getXAxisDateByDateType(dateType);
        result.put("legend",legend);
        result.put("data",data);
        result.put("xAxis",xAxis);
        return result;
    }

    /**
     * 根据日期类型获取日期
     * @param dateType 日期类型
     * @return List<String>
     */
    public List<String> getXAxisDateByDateType(int dateType){
        List<String> result=new ArrayList<>();
        if (dateType==1){
            //获取近一个月中的日期
            result= DateUtil.getNearlyMonthDates();
        }else if (dateType==2){
            //获取当前时间为第几周
            int nowWeek=DateUtil.getWeek(new Date());
            for (int i=1;i<8;i++){
                result.add("第"+String.valueOf(nowWeek-7+i)+"周");
            }
        }else if (dateType==3){
            //获取近一年的月份
            result=DateUtil.getNearlyYearDates();
        }
        return result;
    }

}
